home *** CD-ROM | disk | FTP | other *** search
/ CD Ware Multimedia 1995 May / cd Ware (Juegos) Epimundo.iso / DOS / C / COP.ZIP / COP.H < prev    next >
Encoding:
C/C++ Source or Header  |  1992-07-22  |  7.4 KB  |  248 lines

  1. /*
  2.     cop.h -- C Object Programming v3.02:
  3.         encapsulation, inheritance,
  4.         and polymorphism.
  5.  
  6.     (C) Copyright 1992  John W. Small
  7.     All rights reserved
  8.  
  9.     PSW / Power SoftWare
  10.     P.O. Box 10072
  11.     McLean, Virginia 22102 8072 USA
  12.     Voice: (703) 759-3838
  13. */
  14.  
  15. /* LINTLIBRARY */
  16.  
  17. #ifndef COP_H
  18. #define COP_H
  19.  
  20. #ifndef MALLOC
  21. #include <stdlib.h>
  22. #define MALLOC(size)  malloc(size)
  23. #define    FREE(ptr)  free(ptr)
  24. #endif
  25.  
  26. #define struct_vFt_forward(structType)  \
  27.     typedef struct structType ## _vFt_  \
  28.     structType ## _vFt, * structType ## _vFT
  29.  
  30. #define struct_(structType)  \
  31.     typedef struct structType ## _ structType;  \
  32.     struct structType ## _
  33. #define vFT_decl(structType)  \
  34.     structType ## _vFT vFT
  35. #define polymorphic()  void * descendanT
  36. #define poly_assign(thiS,descendanT_0)  \
  37.     thiS->descendanT = descendanT_0
  38. #define derivedThiS(thiS,derivedStructType)  \
  39.     ((derivedStructType *) (thiS->descendanT))
  40. #define inherit(baseStructType)  \
  41.     baseStructType baseStructType ## Base
  42. #define baseThiS(thiS,baseStructType)  \
  43.     (&((thiS)->baseStructType ## Base))
  44. #define polyBaseThiS_0_decl(structFncPrefix,  \
  45.     polyBaseStructType,structType) extern  \
  46.     polyBaseStructType * structFncPrefix ##\
  47.     _ ## polyBaseStructType ## ThiS_0  \
  48.     (structType * thiS_0)
  49. #define polyBaseThiS_0_def(structFncPrefix,  \
  50.     polyBaseStructType, structType)  \
  51.     polyBaseStructType * structFncPrefix ##\
  52.     _ ## polyBaseStructType ## ThiS_0  \
  53.     (structType * thiS_0)  {  \
  54.     return (thiS_0? structFncPrefix ##\
  55.     _ ## polyBaseStructType ## ThiS(thiS_0)  \
  56.     : (polyBaseStructType *)0); }
  57.  
  58. #define vbInherit(vbaseStructType)  \
  59.     vbaseStructType * vbaseStructType ## BasE
  60. #define vbThiS(thiS,vbaseStructType)  \
  61.     (thiS)->vbaseStructType ## BasE
  62. #define vbDerivedThiS(thiS,derivedStructType)  \
  63.     derivedThis(thiS,derivedStructType)
  64. #define struct_vbc(structType)  \
  65.     typedef struct vbh ## structType ##\
  66.     _ vbh ## structType;  \
  67.     struct vbh ## structType ## _
  68. #define vbh_decl(structType)  \
  69.     structType host
  70. #define vb_decl(vbaseStructType)  \
  71.     vbaseStructType vb ## vbaseStructType
  72. #define vbhThiS(vbcThiS)
  73.  
  74. #define static_def(structType)  \
  75.     structType ## _static structType ## Static
  76. #define struct_static(structType)  \
  77.     typedef struct structType ## _static_  \
  78.     structType ## _static;  \
  79.     extern structType ## _static  \
  80.     structType ## Static;  \
  81.     struct structType ## _static_
  82. #define static_ref(structType,field)  \
  83.     structType ## Static. ## field
  84.  
  85. #define struct_vFt(structType)  \
  86.     struct structType ## _vFt_
  87. #define vFt_def(overridingStructType,  \
  88.     spawningStructType)  \
  89.     spawningStructType ## _vFt  \
  90.     overridingStructType ## _ ##\
  91.     spawningStructType ## _vFt
  92. #define vFt_decl(overridingStructType,  \
  93.     spawningStructType) extern  \
  94.     vFt_def(overridingStructType,  \
  95.     spawningStructType)
  96. #define vF_decl(returnValue,fncName,  \
  97.     formalParams) \
  98.     returnValue (* fncName ) formalParams
  99.     /* formalParams: (structType * thiS,...) */
  100. #define vF_value(overridingStructFncPrefix,  \
  101.     spawningStructFncPrefix, fncName)  \
  102.     overridingStructFncPrefix ## _ ##\
  103.     spawningStructFncPrefix ## _ ## fncName
  104.  
  105. #define vf_def(returnValue,  \
  106.     overridingStructFncPrefix,  \
  107.     spawningStructFncPrefix, fncName,  \
  108.     formalParams)   \
  109.     returnValue overridingStructFncPrefix ## _ ##\
  110.     spawningStructFncPrefix ## _ ## fncName  \
  111.     formalParams
  112.     /* formalParams: (structType * thiS, ...) */
  113. #define vf_decl(returnValue,  \
  114.     overridingStructFncPrefix, \
  115.     spawningStructFncPrefix, fncName,  \
  116.     formalParams)  \
  117.     extern vf_def(returnValue,  \
  118.     overridingStructFncPrefix,  \
  119.     spawningStructFncPrefix,fncName,  \
  120.     formalParams)
  121.     /* formalParams: (structType * thiS, ...) */
  122. #define vf_runTimeBind(spawningStructThiS, fncName,  \
  123.     actualParams)  \
  124.     (*((spawningStructThiS)->vFT->fncName))  \
  125.     actualParams
  126.     /* actualParams: (spawningStructThiS, ...) */
  127. #define vf_compileTimeBind(  \
  128.     overridingStructFncPrefix,  \
  129.     spawningStructFncPrefix, fncName,  \
  130.     actualParams)  \
  131.     overridingStructFncPrefix ## _ ##\
  132.     spawningStructFncPrefix ## _ ## fncName  \
  133.     actualParams
  134.     /* actualParams: (spawningStructThiS, ...) */
  135.  
  136. #define vFT0(spawningStructType)  \
  137.     ((spawningStructType ## _vFT)0)
  138. #define vFT_0_name(spawningStructType)  \
  139.     spawningStructType ## _vFT_0
  140. #define vFT_0_decl(spawningStructType)  \
  141.     spawningStructType ## _vFT  \
  142.     spawningStructType ## _vFT_0
  143. #define vFT_value(overridingStructType,  \
  144.     spawningStructType)  \
  145.     (& overridingStructType ## _ ##\
  146.     spawningStructType ## _vFt)
  147. #define vFT_assign(overridingStructType,  \
  148.     spawningStructType, spawningStructThiS,  \
  149.     spawningStructType_vFT_0)  \
  150.     spawningStructThiS->vFT =  \
  151.     (spawningStructType_vFT_0?   \
  152.     spawningStructType_vFT_0 :  \
  153.     vFT_value(overridingStructType,  \
  154.     spawningStructType))
  155.  
  156. #define vbThiS_name(vbaseStructType)  \
  157.     vbaseStructType ## ThiS
  158. #define vbThiS_decl(vbaseStructType)  \
  159.     vbaseStructType * vbaseStructType ## ThiS
  160. #define vbDescendanT_0_decl(vbaseStructType)  \
  161.     void * vbaseStructType ## DescendanT_0
  162. #define vbDescendanT_value(vbaseStructType,thiS)  \
  163.     (vbaseStructType ## DescendanT_0 ? \
  164.     vbaseStructType ## DescendanT_0 : thiS)
  165.  
  166. #define struct_initVFTs_def(structFncPrefix,  \
  167.     formalParams)  void PT_ ##\
  168.     structFncPrefix ## _initVFTs formalParams
  169. /* formalParams:
  170.     (structType * thiS
  171.         // if polymorphic
  172.         , void * descendanT_0
  173.         // if vf's are introduced at structType
  174.         , vFT_0_decl(structType)
  175.         // The following depend on the hierarchy
  176.         , vFT_0_decl(baseStructType)
  177.         , vbThiS_decl(vbaseStructType)
  178.         , vbDescendanT_0_decl(vbaseStructType)
  179.         , vFT_0_decl(vbaseStructType)
  180.         , vFT_0_decl(subbaseStructType)
  181.         , ...
  182.     )
  183. */
  184. #define struct_initVFTs_decl(structFncPrefix,  \
  185.     formalParams) extern struct_initVFTs_def  \
  186.     (structFncPrefix,formalParams)
  187. #define struct_initVFTs(structFncPrefix, actualParams)\
  188.     PT_ ## structFncPrefix ## _initVFTs \
  189.     actualParams
  190. /* actualParams:
  191.     (thiS
  192.         , descendanT_0
  193.         , vFT0(structType) or
  194.         vFT_value(derivedStructType,structType)
  195.         , vFT0(baseStructType) or
  196.         vFT_value(structType,baseStructType)
  197.         , vbThiS_name(vbaseStructType)
  198.         , vbDescendanT_value(vbaseStructType,thiS)
  199.         , vFT0(vbaseStructType) or
  200.         vFT_value(structType,vbaseStructType)
  201.         , ...
  202.     )
  203. */
  204.  
  205. #define struct_init_def(structType, structFncPrefix, \
  206.     structFncOvrLdSuffix, formalParams)  \
  207.     structType * PT_ ## structFncPrefix ##\
  208.     _init ## structFncOvrLdSuffix formalParams
  209. /* formalParams:
  210.     (structType * thiS_0
  211.         , unsigned nobj
  212.         // location of virtual base:
  213.         , vbThiS_decl(vbaseStructType)
  214.         , ... initializers
  215.     )
  216. */
  217. #define struct_init_decl(structType, structFncPrefix, \
  218.     structFncOvrLdSuffix, formalParams)  \
  219.     extern struct_init_def(structType, \
  220.     structFncPrefix,structFncOvrLdSuffix,  \
  221.     formalParams)
  222. #define struct_init(structFncPrefix,  \
  223.     structFncOvrLdSuffix,actualParams)  \
  224.     PT_ ## structFncPrefix ## _init ##\
  225.     structFncOvrLdSuffix  actualParams
  226. /* actualParams:
  227.     (thiS_0, nobj
  228.         , vbThiS_name(vbaseStructType)
  229.         , ... initializers
  230.     )
  231. */
  232.  
  233. #define struct_destruct_def(structType,  \
  234.     structFncPrefix) void PT_ ##\
  235.     structFncPrefix ## _destruct \
  236.     (structType * thiS_0, unsigned nobj,  \
  237.     int malloced)
  238. #define struct_destruct_decl(structType,  \
  239.     structFncPrefix) extern struct_destruct_def \
  240.     (structType, structFncPrefix)
  241. #define struct_destruct(structFncPrefix,  \
  242.     thiS_0, nobj, malloced)     \
  243.     PT_ ## structFncPrefix ##\
  244.     _destruct (thiS_0,nobj,malloced)
  245.  
  246.  
  247. #endif
  248.